package database;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import data.User;

public class UsersDAO {
	
	private Database db;
	
	/**
	 * Creates a single static connection for the server to grab information.
	 * All SQL statements related to the users will be processed through
	 * UsersDAO.
	 * 
	 * @throws ClassNotFoundException
	 *             thrown if it can't find the driver
	 * @throws SQLException
	 *             thrown if the SQL is bad.
	 */

	public UsersDAO(Database in_db) throws ClassNotFoundException, SQLException {
		db = in_db;
	}
	
	/**
	 * Creates a new user with a given password
	 * @param username new user information
	 * @param password new user information
	 * @param firstName new user information
	 * @param lastName new user information
	 * @param email new user information
	 */
	public void createUser(String username, String password, String firstName,
			String lastName, String email, int indexedRecords) {
		
		try{
			String sql = "INSERT INTO users (username, password, firstName, lastName, email, indexedRecords)"
					+ "VALUES(?, ?, ?, ?, ?, ?)";

			PreparedStatement pStmt = db.getConnection().prepareStatement(sql);

			pStmt.setString(1, username);
			pStmt.setString(2, password);
			pStmt.setString(3, firstName);
			pStmt.setString(4, lastName);
			pStmt.setString(5, email);
			pStmt.setInt(6, indexedRecords);
			
			pStmt.executeUpdate();
		}catch(SQLException e){
			e.printStackTrace();
		}
}

	/**
	 * readUser returns a user object that can be manipulated and if it needs to
	 * be updated use updateUser
	 * 
	 * @param username
	 *            used to find the right user
	 */
	public User readUser(String in_username, String in_password) {
		try{
			String sql = "SELECT * FROM users WHERE username=? AND password =?";

			PreparedStatement pStmt = db.getConnection().prepareStatement(sql);

			pStmt.setString(1, in_username);
			pStmt.setString(2, in_password);
			
			ResultSet rs = pStmt.executeQuery();
			
			if(rs.next()){
				String username = rs.getString("username");
				String password = rs.getString("password");
				String firstName = rs.getString("firstName");
				String lastName = rs.getString("lastName");
				String email = rs.getString("email");
				int indexedRecords = rs.getInt("indexedRecords");
				
				User user = new User(username, password, firstName, lastName, email, indexedRecords);
				
				return user;
			}else{
				User user = new User();
				return user;
			}
			
		}catch(SQLException e){
			e.printStackTrace();
			return null;
		}
		
	}

	/**
	 * updateUser is used to update information related to a specific user
	 * 
	 * @param in_user
	 *            this is the user we want to update
	 */
	public void updateUser(User in_user) {
		try{
			
			String sql = "UPDATE users SET username = ?, password = ?, "
				+ "firstName = ?, lastName = ?, email = ?, indexedRecords = ?";

			PreparedStatement pStmt = db.getConnection().prepareStatement(sql);

			pStmt.setString(1, in_user.getUsername());
			pStmt.setString(2, in_user.getPassword());
			pStmt.setString(3, in_user.getFirstName());
			pStmt.setString(4, in_user.getLastName());
			pStmt.setString(5, in_user.getEmail());
			pStmt.setInt(6, in_user.getIndexedRecords());
			
			pStmt.executeUpdate();
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
}
